<template>
  <div class="detail-wrap-order">
    <div class="detail-wrap-left">
      <el-scrollbar>
        <ul class="stage-list">
          <li
            class="stage-item cursor-move handle"
            :class="item.isActive ? 'stage-item-active' : ''"
            v-for="(item, index) in taskList"
            :key="item.id + 'stage'"
            @click="() => activeHandler(index)"
          >
            <div class="stage-cot">
              <div class="title-box">
                <!-- <el-input placeholder="请输入内容" v-model="item.name" /> -->
                <span class="title-cot">{{ item.name }}</span>
              </div>
            </div>
            <!-- <div class="stage-time">
              <el-icon><Calendar /></el-icon>
              <span>{{ item?.planStartTime ? dayJs(item?.planStartTime).format("YYYY/MM/DD") : "--" }}</span>
              <span class="stage-line">-</span>
              <span>{{ item?.planEndTime ? dayJs(item?.planEndTime).format("YYYY/MM/DD") : "--" }}</span>
            </div> -->
            <div class="status" :style="`color: ${taskStatusColor(item.status)}`">
              {{ getDictItem("orderStatus", item.status) }}
            </div>
          </li>
        </ul>
      </el-scrollbar>
    </div>
    <div class="detail-wrap-content">
      <div class="detail-wrap-content-bottom">
        <el-scrollbar ref="scrollbarContentRef">
          <component :is="Devise" :detail="detail" :subtask="subStage"></component>
        </el-scrollbar>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, inject, computed, watch, onUnmounted } from "vue";
// import dayJs from "dayjs";
import { useDict } from "@/hooks/useDict";
import mittBus from "@/utils/mittBus";
// import Upload from "@/components/Upload/upload.vue";
// import { PROJECT } from "@/constants";
// import { ElMessage } from "element-plus";
import Devise from "./content/devise.vue";
// componen
// api
import { getOrderDetail } from "@/api/modules/order";
import { getListAllUser } from "@/api/modules/sys";

const orderId = inject("orderId");
// hook
const { getDictItem } = useDict(["orderStatus"]); // "projectStage", "taskPriorityId", getDict
// 变量
const viewUserList = ref([]);
let taskList = ref([]);
let taskModelId = ref("");
let taskOutFile = ref([]);
let currentTask = ref({}); // 当前一级
let currentTaskTwo = ref({}); // 当前二级
let currentTaskThree = ref({}); // 当前三级
const scrollbarRef = ref(null);
const scrollbarContentRef = ref(null);
let detail = ref({});
// const urlResource = window?.staticConfig.urlResource;
// TODO 状态记录
let activeList = ref({
  one: 0,
  two: 0,
  three: 0
});
// computed
const subStage = computed(() => {
  return taskList.value[taskList.value.findIndex(item => item.isActive)]?.subtask || [];
});
// computed
// const currentActivePage = computed(() => {
//   scrollbarRef?.value?.setScrollTop(0);
//   const obj = menuLeft.value.find((item: any) => item.isActive);
//   switch (obj.value) {
//     case 1:
//       return OutLine;
//     case 2:
//       return Custom;
//     case 3:
//       return Car;
//     default:
//       return "OutLine";
//   }
// });
// watchEffect
watch(
  () => subStage.value,
  () => {
    taskModelId.value = activeList.value.two ? activeList.value.two : subStage?.value[0]?.id;
    // 当前二级数据
    currentTaskTwo.value = activeList.value.two
      ? subStage?.value[subStage?.value.findIndex(item => item.id === activeList.value.two)]
      : subStage?.value[0];
    // 当前三级的outcome数据
    taskOutFile.value = activeList.value.two
      ? subStage?.value[subStage?.value.findIndex(item => item.id === activeList.value.two)]?.outcome
      : subStage?.value[0]?.outcome || [];

    // 三级初始选中
    taskOutFile.value.forEach((item, i) => {
      item.isActive = i === activeList.value.three;
    });
    // 当前三级数据选中数据
    currentTaskThree.value = taskOutFile.value[activeList.value.three];
  }
);
// 方法
const taskStatusColor = (val: string) => {
  switch (val) {
    case "1814101398665560064": // 作废
    case "1814101293036208128": // 未开始
      return "#999999 ";
    case "1814101313978368000": // 进行中
      return "#5378F6";
    case "1814101348577181696": // 已暂停
      return "#FF5C93";
    case "1814101371838791680": // 已完成
      return "#74D98C";
  }
};
const getListAllUserFn = async () => {
  const { data } = await getListAllUser();
  viewUserList.value = data;
};
// const addProjectPlanFn = async file => {
//   try {
//     await addProjectPlan({
//       projectId,
//       typeId: currentTaskThree.value.typeId,
//       name: file[0].name,
//       path: file[0].path,
//       originalFileName: file[0].originalFileName
//     });
//     await addProjectTaskSaveOutcomeInfo({
//       taskId: taskModelId.value,
//       typeId: currentTaskThree.value.typeId
//     });
//     mittBus.emit("ProjectPlanRefresh");
//     fileList.value = [];
//     ElMessage.success("上传成功");
//   } catch (e) {
//     console.log(e);
//   }
// };
const setScrollLeft = (val: number) => {
  scrollbarRef?.value?.setScrollLeft(val);
};
// 获取订单详情
const getOrderDetailFn = async (id, isActive = true) => {
  let { data } = await getOrderDetail({ id });
  if (isActive) {
    taskList.value = data.orderTaskList;
    taskList.value.forEach((item, i) => {
      item.isActive = i === activeList.value.one;
    });
    currentTask.value = taskList.value[activeList.value.one];
  }
  detail.value = data;
};

const activeHandler = (index: number) => {
  taskList.value.forEach((item, i) => {
    item.isActive = i === index;
  });
  setScrollLeft(0);
  currentTask.value = taskList.value[index];
  // 状态保持
  activeList.value.one = index;
  activeList.value.two = 0;
  activeList.value.three = 0;
};

onMounted(() => {
  getOrderDetailFn(orderId);
  getListAllUserFn();
});
mittBus.on("OrderRefresh", () => {
  getOrderDetailFn(orderId);
});
onUnmounted(() => {
  mittBus.off("OrderRefresh");
});
</script>
<style lang="scss" scoped>
.detail-wrap-order {
  display: flex;
  justify-content: space-between;
  height: 100%;
  :deep(.el-scrollbar) {
    .el-scrollbar__view {
      height: auto;
    }
  }
  ul,
  li {
    padding: 0;
    margin: 0;
    list-style: none;
  }
  .stage-list,
  .stage-item {
    padding: 0;
    margin: 0;
    list-style: none;
  }
  p {
    margin: 0;
  }
  i {
    font-style: normal;
  }

  // :deep(.el-input__wrapper) {
  //   display: flex;
  //   height: 52px;
  //   line-height: 52px;
  //   background: #f7fcfe;
  //   box-shadow: none;
  //   .el-input__suffix-inner > :first-child {
  //     margin-right: 10px;
  //     margin-left: 0;
  //   }
  //   .el-input__inner {
  //     width: auto;
  //     height: 100%;
  //     font-size: 14px;
  //     font-weight: 600;
  //     color: rgb(51 51 51 / 100%);
  //   }
  // }
  // :deep(.el-select) {
  //   width: auto;
  //   min-width: 100px;
  //   .el-input.is-focus {
  //     .el-input__wrapper {
  //       box-shadow: none !important;
  //     }
  //   }
  //   .el-input__wrapper.is-focus {
  //     box-shadow: none !important;
  //   }
  // }
  .stage-list {
    padding-bottom: 1px;
    .stage-item {
      position: relative;
      box-sizing: border-box;
      display: flex;
      flex-flow: column nowrap;
      padding: 16px;
      margin: 20px 30px;
      cursor: pointer;
      background: $project-background;
      border-radius: 6px;
      .status {
        position: absolute;
        top: 0;
        right: 0;
        min-width: 56px;
        padding: 2px 7px;
        font-size: 14px;
        font-weight: 600;
        color: #999999;
        background: #ffffff;
        border-radius: 0 0 0 10px;
      }
      .stage-cot {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .stage-time {
        display: flex;
        align-items: center;
        margin-top: 0;
        font-size: 12px;
        color: #999999;
        :deep(.el-date-editor) {
          .el-range-input {
            cursor: pointer;
          }
        }
        .el-icon {
          margin-right: 4px;
        }
        .stage-line {
          padding: 0 6px;
        }
      }
    }
    .drop-icon {
      display: flex;
      flex: 1;
      justify-content: space-between;
      width: 100%;
      margin-bottom: 5px;
    }
    .title-box {
      display: flex;
      flex: 3;
      align-items: center;
      height: 46px;
      overflow: hidden;
      font-size: 16px;
      font-weight: 600;
      color: #333333;
      text-align: left;
      :deep(.el-input) {
        display: none;
        font-size: 16px;
        border: 0;
        box-shadow: none;
        .el-input__wrapper {
          width: 100%;
          padding: 0;
          border: 0;
          box-shadow: none;
          .el-input__inner {
            height: 32px;
            padding: 0;
            font-size: 16px;
            font-weight: 600;
            line-height: 32px;
            color: #333333;
            border: 0;
            box-shadow: none;
          }
        }
      }
      .title-cot {
        display: block;
        width: 100%;
        height: 32px;
        overflow: hidden;
        line-height: 32px;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .stage-item-active {
      box-sizing: border-box;

      // outline: 1px solid var(--el-color-primary);
      background: var(--el-color-primary) !important;
      .title-cot {
        color: #ffffff;
      }
      .stage-time {
        color: #ffffff;
      }
      .status {
        color: #74d98c;
      }
    }
  }
  .detail-wrap-left {
    flex: 0 0 300px;
    overflow: hidden;
    background: #ffffff;
  }
  .detail-wrap-right {
    display: flex;
    flex: 0 0 340px;
    flex-flow: column nowrap;
    overflow: hidden;
    .task-info-wrap {
      flex: 1.4;
      padding-bottom: 30px;
      padding-left: 30px;
      overflow: hidden;
      background: #ffffff;
    }
    .task-info {
      box-sizing: border-box;
      display: flex;
      flex-flow: column nowrap;
      height: 100%;
      &-title {
        flex: 0 0 64px;
        font-size: 16px;
        font-weight: 600;
        line-height: 64px;
        color: #333333;
      }
      &-ul {
        flex: 1;
        li {
          display: flex;
          flex-flow: column nowrap;
          margin-top: 20px;
          span:first-child {
            margin-bottom: 20px;
            font-size: 16px;
            font-weight: 400;
            color: #666666;
          }
          > span:last-child {
            display: flex;
            align-items: center;
            font-size: 16px;
            font-weight: 600;
            color: #666666;
            img {
              width: 32px;
              height: 32px;
              margin-right: 10px;
              border-radius: 50%;
            }
          }
        }
        .upload-wrap {
          display: flex;
          align-items: center;
          justify-content: space-between;
          span:first-child {
            box-sizing: border-box;
            flex: 2;
            height: 40px;
            padding: 0 20px;
            margin: 0;
            margin-right: 10px;
            overflow: hidden;
            line-height: 40px;
            text-overflow: ellipsis;
            white-space: nowrap;
            background: $project-background;
            border-radius: 4px;
          }
          :deep(.upload-box-s) {
            flex: 1;
            margin-right: 20px;
            .el-upload__tip {
              margin: 0;
            }
          }
        }
      }
    }
    .task-info-bottom {
      flex: 1;
      margin-top: 10px;
    }
  }
  .detail-wrap-content {
    display: flex;
    flex: 1;
    flex-flow: column nowrap;
    margin-left: 10px;
    overflow: hidden;
    .detail-wrap-content-bottom {
      flex: 1;
      overflow: hidden;
    }
  }
}
</style>
